#include "Stdafx.h"
#include "BillBoard.h"

namespace Pulpy
{
	void BillBoard::draw()
	{
			
		glDisable(GL_CULL_FACE);
		glEnable (GL_BLEND); 
		glEnable(GL_TEXTURE_2D);
		//glEnable(GL_ALPHA_TEST);
		//glDisable(GL_COLOR_MATERIAL);
		//glAlphaFunc(GL_GREATER,0);
		//glDisable(GL_DEPTH_TEST);
		glEnable(GL_DEPTH_TEST);
		glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
			
		//glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_DST_ALPHA);
		//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glBlendFunc(GL_DST_COLOR, GL_ZERO);
		glPushMatrix();
			static float modelview[16];
			glGetFloatv( GL_MODELVIEW_MATRIX ,  modelview); // get current modelview matrix 
			for( int i=0; i<3; i++ ) // undo all transformation (face camera) 
				for( int j=0; j<3; j++ ) { 
					if ( i==j ) modelview[i*4+j] = 1.0; 
					else 
						modelview[i*4+j] = 0.0; 
			}
				
			glLoadMatrixf(modelview);// set modelview with no rotations & scaling

			glBindTexture(GL_TEXTURE_2D, _Image.texID);
				
			glTranslatef(getPosition().getX(),getPosition().getY(),getPosition().getZ());
			glScalef(_Scale,_Scale,_Scale);
			glBegin(GL_QUADS);
				glTexCoord2f( 0.0f, 0.0f ); glVertex3f(-getWidth()/2, 0.0f, 0 );
				glTexCoord2f( 1.0f, 0.0f ); glVertex3f(getWidth()/2, 0.0f, 0);
				glTexCoord2f( 1.0f, 1.0f ); glVertex3f( getWidth()/2, getHeight(), 0 );
				glTexCoord2f( 0.0f, 1.0f ); glVertex3f(-getWidth()/2, getHeight(), 0);
				if(_Cross)
				{
					glTexCoord2f( 0.0f, 0.0f ); glVertex3f(0, 0.0f, -getDepth()/2);
					glTexCoord2f( 1.0f, 0.0f ); glVertex3f(0, 0.0f, getDepth()/2);
					glTexCoord2f( 1.0f, 1.0f ); glVertex3f(0 , getHeight(), getDepth()/2 );
					glTexCoord2f( 0.0f, 1.0f ); glVertex3f(0, getHeight(), -getDepth()/2);
				}
			glEnd();
				
			glDisable(GL_TEXTURE_2D);
			glDisable(GL_BLEND);
			glDisable(GL_ALPHA_TEST);
			glEnable(GL_CULL_FACE);
		glPopMatrix();
	}
}